(function(){
   'use strict';

   var multiCTC = angular.module('multiCTC',['zhx.multi.ctc']);

   multiCTC.controller('multiCtcMainController',['$scope','$http','$interval',function($scope,$http,$interval){


       /**
        * THESE ARE CREATED FOR TESTING API & DATA
        */
       var __newId = function () {
           var date, seed, char, randomChar = "", string;
           date = new Date().getTime();
           seed = parseInt(Math.random(1, 1000) * 1000);
           char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
           for (var i = 0; i < 5; i++) {
               randomChar += char.charAt(Math.floor(Math.random() * 52));
           }
           string = String(seed + date) + randomChar;
           return 'bus-' + string.substr(5);
       };
       var __newBusNo = function () {
           var randomChar = "陕A", char = "1234567890";
           for (var i = 0; i < 5; i++) {
               randomChar += char.charAt(Math.floor(Math.random() * 10));
           }
           return randomChar;
       };
       var __getRandomNo = function (Max, Min) {
           var Range = Max - Min;
           var Rand = Math.random();
           return (Min + Math.round(Rand * Range));
       };
       var __getTheNo = function (chars) {
           var char = chars;
           return char.charAt(Math.floor(Math.random() * chars.length));
       };

       var __getRate = function () {
           return Math.random().toString().substring(5, '.');
       };

       var __getRandomLineId = function(){

           var lineId = [
               'xevruNbyVzLrbYG2',
               'AxKx8ydmypVzVRWv',
               'JoYUsUq8KApPBrFz',
               'vHxw3WvYBmKiTeqR',
               'TDPMhAsWk3BsXUhR',
               'g7stCLqPauKcjzTP'
           ];

           return lineId[ __getRandomNo(0,5) ];

       };

       //动态新增BUS模拟数据,会随机移动,到头了重头再跑
       var __addNewBus = function () {

           var busObject = {
               resData: {
                   "lineType": __getTheNo("12"),
                   "busUuid": __newId(),
                   "busPlate": __newBusNo(),
                   "status": __getTheNo("0102030809"),
                   // "status": "0123489",
                   "planTime": "2016-10-11 16:05",
                   "rate": __getRate()
               },
               lineId: __getRandomLineId()
           };

           $scope.ctcOptions.busData = busObject;

           var intervalTime = __getRandomNo(3000, 50000);

           $interval(function(){

               var randomRate = __getRandomNo(0.000, 0.003);

               if (busObject.resData.status === "0") {

                   var percent = parseFloat(busObject.resData.rate);
                   if (percent <= 1) {
                       percent += randomRate;
                       busObject.resData.rate = percent.toFixed(3)
                   } else {
                       console.log('finished running and starting a new circle');
                       busObject.resData.rate = "0.000";
                   }

               }

               $scope.ctcOptions.busData = busObject;

               intervalTime = __getRandomNo(3000, 50000);

           },intervalTime );

           return busObject;
       };

       $scope.ctcOptions = {
           ctcData : null,
           busData : {
               resData : null,
               lineId : null
           },
           isColumnScreen : false,
           lineKeyMap : {},
           busKeyMap : {}
       };

       $scope.createNewCTC = function(){
           /**
            * 创建线路
            */
           $http.get('data/line-data.json').then(function(res){

               $scope.ctcOptions.ctcData = res.data.data;

           });

       };

       $scope.busCount = 0;

       $scope.addBus = function(){

           /**
            * 模拟添加多辆车
            */
           var addBusInterval = $interval(function(){

               __addNewBus();

               $scope.busCount++;

               if( $scope.busCount === 150 ) $interval.cancel(addBusInterval);

           },50);

       };


   }]);

})();